# -*- coding: utf-8 -*-
from odoo import api, fields, models, tools
import odoo.addons.decimal_precision as dp

class ZfyAccountAnalyticInvoice(models.Model):
    _inherit='account.analytic.account'

    @api.depends('recurring_invoice_line_ids.price_subtotal')
    def _compute_price_total(self):
        for rcd in self:
            if rcd.recurring_invoice_line_ids:
                rcd.price_total = sum(line.price_subtotal for line in rcd.recurring_invoice_line_ids)
    #     domain = [('model', '=', 'ir.ui.view'), ('res_id', 'in', self.ids)]
    #     res = self.search_read(domain, ['res_id'],order='id desc')
    #         # view = self.browse(data['res_id'])
    #         # view.model_data_id = data['id']
    #         # return view
    # price_total = fields.Monetary(string=u'总计',store=True)
    price_total = fields.Monetary(string=u'总计',compute='_compute_price_total')

    # @api.model
    # def search_read(self, domain=None, fields=None, offset=0, limit=None,order=None):
    #
    #     records = self.search([], offset=0, limit=None,order=None)
    #     for rcd in records:
    #         if rcd.recurring_invoice_line_ids:
    #             rcd.price_total = sum(line.price_subtotal for line in rcd.recurring_invoice_line_ids)
    #     if not records:
    #         return []
    #
    #     if fields and fields == ['id']:
    #         # shortcut read if we only want the ids
    #         return [{'id': record.id} for record in records]
    #
    #     # read() ignores active_test, but it would forward it to any downstream search call
    #     # (e.g. for x2m or function fields), and this is not the desired behavior, the flag
    #     # was presumably only meant for the main search().
    #     # TODO: Move this to read() directly?
    #     if 'active_test' in self._context:
    #         context = dict(self._context)
    #         del context['active_test']
    #         records = records.with_context(context)
    #
    #     result = records.read(fields)
    #     if len(result) <= 1:
    #         return result
    #
    #     # reorder read
    #     index = {vals['id']: vals for vals in result}
    #     return [index[record.id] for record in records if record.id in index]


    # @api.model
    # def create(self, vals):
    #     for rcd in self:
    #         if rcd.recurring_invoice_line_ids:
    #             vals['price_total'] = sum(line.price_subtotal for line in rcd.recurring_invoice_line_ids)
    #     return super(ZfyAccountAnalyticInvoice,self).create(vals)
    #
    # @api.model_cr
    # def init(self):
    #     for rcd in self:
    #         para = 0
    #         if rcd.recurring_invoice_line_ids:
    #             para = sum(line.price_subtotal for line in rcd.recurring_invoice_line_ids)
    #         self._cr.execute("""
    #         UPADATE account_analytic_account SET price_total = %s WHERE id = %s
    #         """%(para,rcd.id))


